1 module core.stdc.math;
2 import core.stdc.stddef;
3 
4 
5 enum PI = 3.1415;
6 
7 version(WebAssembly)
8 {
9     extern(C) @nogc nothrow @safe pure
10     {
11         //Double
12         double sqrt(double x){return cast(double)sqrtf(cast(float)x);}
13         double atan2(double y, double x){return cast(double)atan2f(cast(float)y, cast(float)x);}
14         double cos(double x){return cast(double)cosf(cast(float)x);}
15         double acos(double x){return cast(double)acosf(cast(float)x);}
16         double sin(double x){return cast(double)sinf(cast(float)x);}
17         double tan(double x){return cast(double)tanf(cast(float)x);}
18         double cbrt(double x);
19 
20 
21 
22         double fabs(double x){ return x > 0 ? x : -x;}
23         double pow(double x, double y)
24         {
25             if(y == 0) return 1;
26             double ret = x;
27             if(y > 0) foreach(i; 1..y)
28                 ret*=x;
29             else if(x == 0)
30                 return double.infinity;
31             else foreach(i; y..1)
32                 ret/= x;
33             return ret;
34         }
35         double floor(double x){return cast(double)(cast(long)x);}
36         double ceil(double x){return cast(double)(cast(long)(x+0.999999999));}
37         double fmod(double f, double w) 
38         {
39             auto i = cast(int) f;
40             return i % cast(int) w;
41         }
42 
43 
44         //Float
45         float sqrtf(float x);
46         float atan2f(float y, float x);
47         float cosf(float x);
48         float acosf(float x);
49         float sinf(float x);
50         float tanf(float x);
51 
52         float fabsf(float x){return x > 0 ? x : -x;}
53         float powf(float x, float y)
54         {
55             if(y == 0) return 1;
56             float ret = x;
57             foreach(i; 1..y)
58                 ret*=x;
59             return ret;
60         }
61         float floorf(float x){return cast(float)(cast(int)x);}
62         float ceilf(float x){return cast(float)(cast(int)(x+0.999999999));}
63         float fmodf(float x, float denom)
64         { 
65             float div = x / denom;
66             div = div - cast(int)div;
67             return cast(float)(cast(int)div*denom);
68         }
69     }
70 }
71 else
72 {
73     extern(C) extern @nogc @safe nothrow pure
74     {
75         double sqrt(double x);
76         double atan2(double y, double x);
77         double cos(double x);
78         double acos(double x);
79         double sin(double x);
80         double tan(double x);
81         double fabs(double x);
82         double pow(double x, double y);
83         double floor(double x);
84         double ceil(double x);
85         double fmod(double x, double denom);
86         double cbrt(double x);
87         float cbrtf(float x);
88 
89 
90         float sqrtf(float x);
91         float atan2f(float y, float x);
92         float cosf(float x);
93         float acosf(float x);
94         float sinf(float x);
95         float tanf(float x);
96         float fabsf(float x);
97         float powf(float x, float y);
98         float floorf(float x);
99         float ceilf(float x);
100         float fmodf(float x, float denom);
101     }
102 }
103 
104 extern(C) @nogc nothrow @trusted pure:
105 
106 enum int FP_ILOGB0        = int.min;
107 ///
108 enum int FP_ILOGBNAN      = int.min;
109 extern(D) real fmodl()(real x, real y) { return fmod(cast(double) x, cast(double) y); }
110 
111 float remainderf( float x, float y ){assert(0);}
112 double remainder( double x, double y ){assert(0);}
113 real remainderl( real x, real y ){assert(0);}
114 double  remquo(double x, double y, int* quo){assert(0);}
115 float   remquof(float x, float y, int* quo){assert(0);}
116 extern(D) real remquol()(real x, real y, int* quo) { return remquo(cast(double) x, cast(double) y, quo); }
117 extern(D) pure real cbrtl()(real x)   { return cbrt(cast(double) x); }
118 extern(D) pure real modfl()(real value, real* iptr)
119 {
120     double i;
121     double r = modf(cast(double) value, &i);
122     *iptr = i;
123     return r;
124 }
125 
126 pure double  modf(double value, double* iptr){assert(0);}
127 pure float   modff(float value, float* iptr){assert(0);}
128 pure double  nearbyint(double x){assert(0);}
129 pure float   nearbyintf(float x){assert(0);}
130 extern(D) pure real nearbyintl()(real x) { return nearbyint(cast(double) x); }
131 
132 pure float   roundf(float x)
133 { 
134     return ((x - cast(int)x) >= 0.5) ? cast(int)x+1 : cast(int)x;
135 }
136 pure double  round(double x){ return cast(double)roundf(x);}
137 extern(D) pure real roundl()(real x)  { return round(cast(double) x); }
138 
139 long llround(double x)
140 {
141     return ((x - cast(long)x) >= 0.5) ? cast(long)x+1 : cast(long)x;
142 }
143     ///
144 long llroundf(float x){return llroundf(cast(double)x);}
145 ///
146 extern(C) long llroundl(double x) { return llround(cast(double) x); }
147 extern(D) long llroundl()(real x) { return llround(cast(double) x); }
148 
149 
150 pure double  trunc(double x) {return cast(double)(cast(long)x);}
151 ///
152 pure float   truncf(float x) {return cast(float)(cast(int)x);}
153 ///
154 extern(D) pure real truncl()(real x)  { return trunc(cast(double) x); }